package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.CartAddBo;
import com.cskaoyan.bean.bo.CartCheckedBo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.*;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chengyi
 * @date 2022/10/14 15:43
 */
@Service
public class WxCartServiceImpl implements WxCartService {
    @Autowired
    MarketCartMapper cartMapper;

    @Autowired
    MarketAddressMapper addressMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketSystemMapper systemMapper;

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketGoodsProductMapper goodsProductMapper;

    @Autowired
    MarketGoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    WxCouponService wxCouponService;

    @Autowired
    MarketSystemMapper marketSystemMapper;
    private static BigDecimal getAllPrice;
    private static Integer cartAllGoodsCount;


    @Override
    public WxCartDataVo index() {

        Session session = SecurityUtils.getSubject().getSession();
        Integer cartId = (Integer) session.getAttribute("cartId");
        if(cartId!=null){
            //逻辑删除该购物车记录
            MarketCart marketCart = new MarketCart();
            marketCart.setId(cartId);
            marketCart.setDeleted(true);
            cartMapper.updateByPrimaryKeySelective(marketCart);
        }
        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        return getWxCartData(example);


    }

    private WxCartDataVo getWxCartData(MarketCartExample example) {
        //查找购物车的商品信息
        List<MarketCart> marketCarts = cartMapper.selectByExample(example);

        if (marketCarts.size() == 0) {
            return null;
        }

        //计算购物车中四个参数的值
        //获取购物车商品总数量
        int gCount = 0;
        for (int i = 0; i < marketCarts.size(); i++) {
            gCount += marketCarts.get(i).getNumber();
        }

        //获取购物车选中的商品数量
        int checkGCount = 0;
        for (int i = 0; i < marketCarts.size(); i++) {
            if (marketCarts.get(i).getChecked() == true) {
                checkGCount += marketCarts.get(i).getNumber();
            }
        }


        //获取购物车商品的总价格
        BigDecimal gAmount = new BigDecimal(0);
        BigDecimal amount = null;
        for (int i = 0; i < marketCarts.size(); i++) {
            //获取价格
            BigDecimal price = marketCarts.get(i).getPrice();
            //获取数量
            int count = marketCarts.get(i).getNumber();
            BigDecimal number = new BigDecimal(count);
            //乘积
            amount = price.multiply(number);
            //累加
            gAmount = gAmount.add(amount);


        }


        //获取购物车选中商品的价格
        BigDecimal checkGAmount = new BigDecimal(0);
        BigDecimal amount1 = null;
        for (int i = 0; i < marketCarts.size(); i++) {
            if (marketCarts.get(i).getChecked() == true) {
                //获取价格
                BigDecimal price = marketCarts.get(i).getPrice();
                //获取数量
                int count = marketCarts.get(i).getNumber();
                BigDecimal number = new BigDecimal(count);
                //乘积
                amount1 = price.multiply(number);
                //累加
                checkGAmount = checkGAmount.add(amount1);
            }
        }
        getAllPrice = checkGAmount;

        //往WxCartTotalVo对象里赋值
        WxCartTotalVo cartTotalVo = new WxCartTotalVo();
        cartTotalVo.setGoodsCount(gCount);
        cartTotalVo.setCheckedGoodsCount(checkGCount);
        cartTotalVo.setGoodsAmount(gAmount);
        cartTotalVo.setCheckedGoodsAmount(checkGAmount);

        //往WxCartDataVo对象里赋值
        WxCartDataVo dataVo = new WxCartDataVo();
        dataVo.setCartTotal(cartTotalVo);
        dataVo.setCartList(marketCarts);

        //返回参数
        return dataVo;
    }

    /**
     * 编辑购物车
     *
     * @param marketCart
     */
    @Override
    public void update(MarketCart marketCart) {
        //修改更新时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketCart.setUpdateTime(date1);
        //购物车商品的数量要大于1的时候才能减少
        if (marketCart.getNumber() >= 1) {
//            cartMapper.updateBy(wxCartUpdateBo);
            cartMapper.updateByPrimaryKeySelective(marketCart);
        }


    }

    /**
     * 勾选购物车商品
     *
     * @param
     * @return
     */
    @Override
    public WxCartDataVo checked(CartCheckedBo cartCheckedBo) {

        //获取用户id
        MarketUser marketUser = (MarketUser)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();

        //获取productId
        List<Integer> productIds = cartCheckedBo.getProductIds();
        if(productIds.size()==1){
            Integer productId = productIds.get(0);
            //查找对应的商品id
            MarketGoodsProduct marketGoodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
            Integer goodsId = marketGoodsProduct.getGoodsId();
            MarketCart marketCart = new MarketCart();
            marketCart.setUserId(userId);
            marketCart.setGoodsId(goodsId);
            marketCart.setProductId(productId);
            if(cartCheckedBo.getIsChecked()==0){
                marketCart.setChecked(false);
            }else{
                marketCart.setChecked(true);
            }
            int row=cartMapper.updateChecked(marketCart);
        }else{
            //全选或者全不选的业务逻辑
            for (Integer productId : productIds) {

                MarketGoodsProduct marketGoodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
                Integer goodsId = marketGoodsProduct.getGoodsId();
                MarketCart marketCart = new MarketCart();
                marketCart.setUserId(userId);
                marketCart.setGoodsId(goodsId);
                marketCart.setProductId(productId);
                if(cartCheckedBo.getIsChecked()==0){
                    marketCart.setChecked(false);
                }else{
                    marketCart.setChecked(true);
                }
                int row=cartMapper.updateChecked(marketCart);
            }
        }



        //cartMapper.updateByExampleSelective(marketCart,cartExample1);


        //根据用户id查出其购物车对应的商品
        MarketCartExample cartExample = new MarketCartExample();
        cartExample.setDistinct(true);
        MarketCartExample.Criteria cartExampleCriteria = cartExample.createCriteria();
        cartExampleCriteria.andUserIdEqualTo(userId);
        cartExampleCriteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = cartMapper.selectByExample(cartExample);


        WxCartDataVo wxCartDataVo = new WxCartDataVo();
        wxCartDataVo.setCartList(marketCarts);
        //购物车中商品总价格
        BigDecimal goodsAmount=new BigDecimal(0);
        //购物车中商品总数量
        int goodsCount=0;
        //选中的商品的总价格
        BigDecimal checkedGoodsAmount=new BigDecimal(0);
        //选中的商品的数量
        int checkedGoodsCount=0;

        for (MarketCart cart : marketCarts) {
            //单价
            BigDecimal price = cart.getPrice();
            //数量
            Short number1 = cart.getNumber();
            //数量累加
            goodsCount+=number1;
            BigDecimal number = new BigDecimal(number1);
            //相加
            goodsAmount=goodsAmount.add(price.multiply(number));
            if(cart.getChecked()==true){
                //被选中
                checkedGoodsCount+=cart.getNumber();
                checkedGoodsAmount= checkedGoodsAmount.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
            }
        }

        WxCartTotalVo wxCartTotalVo = new WxCartTotalVo();
        wxCartTotalVo.setCheckedGoodsAmount(checkedGoodsAmount);
        wxCartTotalVo.setCheckedGoodsCount(checkedGoodsCount);
        wxCartTotalVo.setGoodsAmount(goodsAmount);
        wxCartTotalVo.setGoodsCount(goodsCount);

        wxCartDataVo.setCartTotal(wxCartTotalVo);

        return wxCartDataVo;


    }

    /**
     * 下单
     *
     * @param cartId
     * @param addressId
     * @param couponId
     * @param userCouponId
     * @param grouponRulesId
     * @return
     */
    @Transactional
    @Override
    public WxCartCheckedVo checkout(Integer cartId, Integer addressId, Integer couponId, Integer userCouponId, Integer grouponRulesId) {
        //获取当前用户id
        MarketUser marketUser = (MarketUser)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();

        //查找地址信息，
        // 1.如果地址id为0，则根据用户id找到其默认的地址
        // 2.如果地址id不为0，则直接根据地址id找到该地址
        MarketAddressExample example = new MarketAddressExample();
        example.setDistinct(true);
        MarketAddressExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if(addressId==0){
            criteria.andUserIdEqualTo(userId);
            criteria.andIsDefaultEqualTo(true);
        }else{
            criteria.andIdEqualTo(addressId);
        }
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(example);
        MarketAddress marketAddress = marketAddresses.get(0);

        //查询商品信息
        //判断请求参数cartId的值
        //若为0，则要查询的商品信息是购物车中被选中的商品的信息
        //若不为0，则是该cartId所对应的商品的id
        //数据全部都在cart中。
        MarketCartExample cartExample = new MarketCartExample();
        cartExample.setDistinct(true);
        MarketCartExample.Criteria cartExampleCriteria = cartExample.createCriteria();
        cartExampleCriteria.andDeletedEqualTo(false);
        if(cartId==0){
            cartExampleCriteria.andUserIdEqualTo(userId);
            cartExampleCriteria.andCheckedEqualTo(true);
        }else{
            cartExampleCriteria.andIdEqualTo(cartId);
        }

        List<MarketCart> marketCarts = cartMapper.selectByExample(cartExample);
        //根据购物车查询出订单总价格
        BigDecimal goodsTotalPrice=new BigDecimal(0);
        for (MarketCart marketCart : marketCarts) {
            Short number = marketCart.getNumber();
            BigDecimal numberBigDecimal = new BigDecimal(number);
            BigDecimal multiply = marketCart.getPrice().multiply(numberBigDecimal);
            goodsTotalPrice=goodsTotalPrice.add(multiply);
        }
        //查询该用户可用的优惠券


        CommonData<CouponMyListVo> commonData = wxCouponService.selectList(cartId,0);
        List<CouponMyListVo> couponList = commonData.getList();
        int availableCouponLength=0;

        //查询优惠券的金额
        BigDecimal couponPrice=new BigDecimal(0);
        if(couponList!=null && couponList.size()>0){
            boolean flag=true;
            //先判断传入的userCouponId和couponId是否是正确的、可用的
            for (CouponMyListVo couponMyListVo : couponList) {
                if(couponMyListVo.getCid()==couponId && couponMyListVo.getId()==userCouponId){
                    couponPrice=couponMyListVo.getDiscount();
                    flag=false;
                }
            }


            if(flag){

                availableCouponLength = couponList.size();
                BigDecimal max=couponList.get(0).getDiscount();
                userCouponId=couponList.get(0).getId();
                couponId=couponList.get(0).getCid();
                for (int i = 1; i < couponList.size(); i++) {
                    if(max.compareTo(couponList.get(i).getDiscount())<0){
                        max=couponList.get(i).getDiscount();
                        userCouponId=couponList.get(i).getId();
                        couponId=couponList.get(i).getCid();
                    }
                }
                couponPrice=max;
            }

        }else{
            availableCouponLength=0;
        }



        // BigDecimal bigDecimal = couponMapper.selectDiscount(userCouponId);
        // if(bigDecimal!=null){
        //     couponPrice=bigDecimal;
        // }


        //查询运费
        String market_express_freight_value="market_express_freight_value";
        MarketSystem marketSystem= marketSystemMapper.selectByKayName(market_express_freight_value);
        String keyValue = marketSystem.getKeyValue();
        BigDecimal freightPrice = new BigDecimal(keyValue);
        //int freight = Integer.parseInt(keyValue);
        //查询免运费最低价格
        String market_express_freight_min="market_express_freight_min";
        MarketSystem marketSystem2= marketSystemMapper.selectByKayName(market_express_freight_min);
        String keyValue2 = marketSystem2.getKeyValue();
        BigDecimal freightMin = new BigDecimal(keyValue2);
        //int freightMin = Integer.parseInt(keyValue2);
        if(goodsTotalPrice.compareTo(freightMin)>0){
            //免运费
            freightPrice=new BigDecimal(0);
        }
        //实际价格
        BigDecimal actualPrice=goodsTotalPrice.add(freightPrice).subtract(couponPrice);
        //订单总价
        BigDecimal orderTotalPrice=actualPrice;
        //用户优惠券 id






        WxCartCheckedVo wxCartCheckedVo = new WxCartCheckedVo();
        wxCartCheckedVo.setCheckedAddress(marketAddress);
        wxCartCheckedVo.setCheckedGoodsList(marketCarts);
        //
        wxCartCheckedVo.setActualPrice(actualPrice);
        wxCartCheckedVo.setAddressId(addressId);
        wxCartCheckedVo.setAvailableCouponLength(availableCouponLength);
        wxCartCheckedVo.setCartId(cartId);
        wxCartCheckedVo.setCouponId(couponId);
        wxCartCheckedVo.setCouponPrice(couponPrice);
        wxCartCheckedVo.setFreightPrice(freightPrice);
        wxCartCheckedVo.setGrouponPrice(new BigDecimal(0));
        wxCartCheckedVo.setGrouponRulesId(0);
        wxCartCheckedVo.setOrderTotalPrice(orderTotalPrice);
        wxCartCheckedVo.setUserCouponId(userCouponId);
        wxCartCheckedVo.setGoodsTotalPrice(goodsTotalPrice);

        //将购物信息存到session中
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("wxCartCheckedVo",wxCartCheckedVo);


        return wxCartCheckedVo;


    }

    /**
     * 商品加购详情
     *
     * @return
     */
    @Override
    public int goodCount() {

        //判断用户是否已登录
        MarketUser marketUser = null;
        try {
            marketUser = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        } catch (Exception e) {
            //未登录
            return 0;
            //e.printStackTrace();
        }
        // if(marketUser ==null){
        //     return 0;
        // }

        MarketCartExample example = new MarketCartExample();
        MarketCartExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(marketUser.getId());
        criteria.andDeletedEqualTo(false);
        //查找购物车的商品信息
        List<MarketCart> marketCarts = cartMapper.selectByExample(example);


        //获取购物车商品总数量
        int gCount = 0;
        for (int i = 0; i < marketCarts.size(); i++) {
            gCount += marketCarts.get(i).getNumber();
        }
        return gCount;
    }

    /**
     * 加入购物车
     *
     * @param cartAddBo
     * @return
     */
    @Override
    public int add(CartAddBo cartAddBo) {
        Integer goodsId = cartAddBo.getGoodsId();
        Short number = cartAddBo.getNumber();
        Integer productId = cartAddBo.getProductId();

        MarketCart marketCart = new MarketCart();
        //根据传入的参数获取购物车里的值
        marketCart.setGoodsId(goodsId);
        marketCart.setNumber(number);
        marketCart.setProductId(productId);


        //从商品中获取goods_sn,goodsName,price,pic_url
        MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
        marketCart.setGoodsSn(marketGoods.getGoodsSn());
        marketCart.setGoodsName(marketGoods.getName());
        marketCart.setPrice(marketGoods.getRetailPrice());
        marketCart.setPicUrl(marketGoods.getPicUrl());

        //获取用户Id
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser user = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = user.getId();

        marketCart.setUserId(userId);

        //specifications
        MarketGoodsProduct marketGoodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
        String[] specifications = marketGoodsProduct.getSpecifications();

        marketCart.setSpecifications(specifications);


        //add_time和update_time
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketCart.setAddTime(date1);
        marketCart.setUpdateTime(date1);

        //查找购物车的商品信息
        MarketCartExample example = new MarketCartExample();
        example.setDistinct(true);
        MarketCartExample.Criteria criteria1 = example.createCriteria();
        criteria1.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = cartMapper.selectByExample(example);


        //如果购物车已经存在相同规格的商品，则数量加1，否则插入数据
        MarketCart marketCart2 = new MarketCart();
        //int count = 0;
        boolean flag=true;
        for (int i = 0; i < marketCarts.size(); ++i) {
            Integer goodsId1 = marketCarts.get(i).getGoodsId();
            Integer productId1 = marketCarts.get(i).getProductId();
            if (goodsId1.equals(goodsId)){
                if(productId1.equals(productId)){
                    Short number1 = marketCarts.get(i).getNumber();
                    int lastNumber = number1 + number;
                    marketCart2.setNumber((short) lastNumber);
                    MarketCartExample.Criteria criteria = example.createCriteria();
                    //short number2 = (short) lastNumber;
                    //criteria.andNumberEqualTo(number2);
                    Integer id = marketCarts.get(i).getId();
                    criteria.andIdEqualTo(id);
                    criteria.andDeletedEqualTo(false);

                    int i1 = cartMapper.updateByExampleSelective(marketCart2, example);
                    flag=false;
                    break;
                }
            }
           // count++;
        }
        if(flag){
                cartMapper.insertSelective(marketCart);
        }
        // if(count >= marketCarts.size()){
        //     cartMapper.insertSelective(marketCart);
        // }


        //获取购物车商品总数量
        int gCount = 0;
        for (int i = 0; i < marketCarts.size(); i++) {
            gCount += marketCarts.get(i).getNumber();
        }
        return gCount;
    }

    /**
     * 立即购买
     *
     * @param cartAddBo
     * @return
     */
    @Transactional
    @Override
    public int fastAdd(CartAddBo cartAddBo) {
        Integer goodsId = cartAddBo.getGoodsId();
        Short number = cartAddBo.getNumber();
        Integer productId = cartAddBo.getProductId();

        MarketCart marketCart = new MarketCart();
        //根据传入的参数获取购物车里的值
        marketCart.setGoodsId(goodsId);
        marketCart.setNumber(number);
        marketCart.setProductId(productId);


        //从商品中获取goods_sn,goodsName,price,pic_url
        MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
        marketCart.setGoodsSn(marketGoods.getGoodsSn());
        marketCart.setGoodsName(marketGoods.getName());
        marketCart.setPrice(marketGoods.getRetailPrice());
        marketCart.setPicUrl(marketGoods.getPicUrl());

        //获取用户Id
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        MarketUser user = (MarketUser) principals.getPrimaryPrincipal();
        Integer userId = user.getId();

        marketCart.setUserId(userId);

        //specifications
        MarketGoodsProduct marketGoodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
        String[] specifications = marketGoodsProduct.getSpecifications();

        marketCart.setSpecifications(specifications);


        //add_time和update_time
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketCart.setAddTime(date1);
        marketCart.setUpdateTime(date1);
        marketCart.setDeleted(true);
        //插入数据
        marketCart.setDeleted(false);
        cartMapper.insertSelective(marketCart);
        Integer id = marketCart.getId();

        //将该id存到session中
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("cartId",id);
        //删除数据
        //cartMapper.deleteByPrimaryKey(id);

        return id;
    }

    /**
     * 删除购物车商品
     * @param map
     * @return
     */
    @Override
    @Transactional
    public WxCartDataVo delete(Map map) {

        //获取用户id
        MarketUser marketUser = (MarketUser)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer userId = marketUser.getId();

        //删除逻辑
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        if(productIds.size()==1){
            Integer productId = productIds.get(0);
            //查找对应的商品id
            MarketGoodsProduct marketGoodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
            Integer goodsId = marketGoodsProduct.getGoodsId();
            MarketCartExample cartExample = new MarketCartExample();
            cartExample.setDistinct(true);
            MarketCartExample.Criteria cartExampleCriteria = cartExample.createCriteria();
            cartExampleCriteria.andUserIdEqualTo(userId);
            cartExampleCriteria.andProductIdEqualTo(productId);
            cartExampleCriteria.andGoodsIdEqualTo(goodsId);
            cartExampleCriteria.andDeletedEqualTo(false);
            MarketCart marketCart = new MarketCart();
            marketCart.setDeleted(true);
            cartMapper.updateByExampleSelective(marketCart,cartExample);

        }else{
            //全选或者全不选的业务逻辑
            for (Integer productId : productIds) {
                MarketGoodsProduct marketGoodsProduct = goodsProductMapper.selectByPrimaryKey(productId);
                Integer goodsId = marketGoodsProduct.getGoodsId();
                MarketCartExample cartExample = new MarketCartExample();
                cartExample.setDistinct(true);
                MarketCartExample.Criteria cartExampleCriteria = cartExample.createCriteria();
                cartExampleCriteria.andUserIdEqualTo(userId);
                cartExampleCriteria.andProductIdEqualTo(productId);
                cartExampleCriteria.andGoodsIdEqualTo(goodsId);
                cartExampleCriteria.andDeletedEqualTo(false);
                MarketCart marketCart = new MarketCart();
                marketCart.setDeleted(true);
                cartMapper.updateByExampleSelective(marketCart,cartExample);
            }
        }
        //获取返回参数
        //根据用户id查出其购物车对应的商品
        MarketCartExample cartExample = new MarketCartExample();
        cartExample.setDistinct(true);
        MarketCartExample.Criteria cartExampleCriteria = cartExample.createCriteria();
        cartExampleCriteria.andUserIdEqualTo(userId);
        cartExampleCriteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = cartMapper.selectByExample(cartExample);


        WxCartDataVo wxCartDataVo = new WxCartDataVo();
        wxCartDataVo.setCartList(marketCarts);
        //购物车中商品总价格
        BigDecimal goodsAmount=new BigDecimal(0);
        //购物车中商品总数量
        int goodsCount=0;
        //选中的商品的总价格
        BigDecimal checkedGoodsAmount=new BigDecimal(0);
        //选中的商品的数量
        int checkedGoodsCount=0;

        for (MarketCart cart : marketCarts) {
            //单价
            BigDecimal price = cart.getPrice();
            //数量
            Short number1 = cart.getNumber();
            //数量累加
            goodsCount+=number1;
            BigDecimal number = new BigDecimal(number1);
            //相加
            goodsAmount=goodsAmount.add(price.multiply(number));
            if(cart.getChecked()==true){
                //被选中
                checkedGoodsCount+=cart.getNumber();
                checkedGoodsAmount= checkedGoodsAmount.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber())));
            }
        }

        WxCartTotalVo wxCartTotalVo = new WxCartTotalVo();
        wxCartTotalVo.setCheckedGoodsAmount(checkedGoodsAmount);
        wxCartTotalVo.setCheckedGoodsCount(checkedGoodsCount);
        wxCartTotalVo.setGoodsAmount(goodsAmount);
        wxCartTotalVo.setGoodsCount(goodsCount);

        wxCartDataVo.setCartTotal(wxCartTotalVo);


        return wxCartDataVo;
    }

    @Override
    public int selectAll() {
        List<MarketCart> marketCarts = cartMapper.selectAllCartList();
        return 0;
    }
}
